<template>
  <div class="login-container">
    <div class="login-content">
      <a class="banner-box" href="" />
      <validation-observer v-slot="{ handleSubmit }" class="login-box">
        <form class="input-wrap" @submit.prevent="handleSubmit(handleLogin)">
          <div class="title">SunMoon</div>
          <div class="sub-title">博客管理平台</div>
          <validation-provider v-slot="{ errors }" rules="required_username">
            <div class="input-box">
              <input
                v-model="form.username"
                class="input"
                name="用户名"
                placeholder="请输入用户名"
                tabindex="1"
                type="text"
              />
            </div>
            <div class="errors-wrap">{{ errors[0] }}</div>
          </validation-provider>
          <validation-provider v-slot="{ errors }" rules="required_password">
            <div class="input-box">
              <input
                ref="password"
                v-model="form.password"
                :type="passwordType"
                class="input"
                name="密码"
                placeholder="请输入密码"
                tabindex="2"
              />
              <span class="show-password" @click="switchPasswordVisibility">
                <svg-icon
                  class="show-password-icon"
                  icon-class="passwordType === 'password' ? 'eye-close' : 'eye'"
                />
              </span>
            </div>
            <div class="errors-wrap">{{ errors[0] }}</div>
          </validation-provider>
          <button :disabled="loading" class="login-button" type="submit">登 录</button>
        </form>
      </validation-observer>
    </div>
  </div>
</template>

<script>
  import { isRightfulName } from '@/util/validate'
  import { extend, ValidationObserver, ValidationProvider } from 'vee-validate'
  import { required } from 'vee-validate/dist/rules'

  extend('required_username', {
    ...required,
    message: '请输入用户名',
  })

  extend('required_password', {
    ...required,
    message: '请输入密码',
  })

  extend('min', {
    validate(value, args) {
      return value.length >= args.length
    },
    message(field, placeholder) {
      return `${field}不能低于${placeholder.length}位`
    },
    params: ['length'],
  })
  extend('isName', {
    validate(value) {
      return isRightfulName(value)
    },
    message: '请输入4到16位的数字或字母或下划线组合的用户名',
  })

  export default {
    name: 'Login',
    components: {
      ValidationProvider,
      ValidationObserver,
    },
    directives: {
      focus: {
        inserted(el) {
          el.querySelector('input').focus()
        },
      },
    },
    data() {
      return {
        form: {
          username: '',
          password: '',
        },
        passwordType: 'password',
        loading: false,
      }
    },
    computed: {},
    watch: {
      $route: {
        handler(route) {
          this.redirect = (route.query && route.query.redirect) || '/'
        },
        immediate: true,
      },
    },
    created() {
      document.body.style.overflow = 'hidden'
    },
    beforeDestroy() {
      document.body.style.overflow = 'auto'
      clearTimeout(this.timer)
    },
    methods: {
      handleLogin() {
        this.loading = true
        this.$store
          .dispatch('user/login', this.form)
          .then((res) => {
            this.$router.push('/')
            this.loading = false
          })
          .catch((err) => {
            console.log(err)
            this.loading = false
          })
      },
      switchPasswordVisibility() {
        this.passwordType === 'password'
          ? (this.passwordType = '')
          : (this.passwordType = 'password')

        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  $bg_c: #fcfaf2;
  $btn_bg_c: #0c0c0c;

  .login-container {
    height: 100vh;
    background: url(~@/assets/images/login-bg.jpg) center center fixed no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
  }

  .login-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 800px;
    height: 400px;
    background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    border-radius: 8px;
    padding: 20px;
    transform: translateY(50%);
  }

  .banner-box {
    flex: 1;
    height: 100%;
    background: url(~@/assets/images/login-bg.jpg) no-repeat;
    background-size: cover;
    border-radius: 8px;
  }

  .login-box {
    width: 320px;
    height: 100%;
  }

  .input-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 20px;

    .title,
    .sub-title {
      color: $btn_bg_c;
    }

    .title {
      font-size: 32px;
    }

    .sub-title {
      margin-bottom: 30px;
      opacity: 0.5;
    }

    .input-box {
      position: relative;
      cursor: pointer;

      .show-password {
        position: absolute;
        top: 10px;
        right: 10px;
        cursor: pointer;
        font-size: 18px;
        user-select: none;
        color: #666;
        transition: all 0.15s;

        &hover {
          color: #999;
        }
      }
    }

    input {
      width: 200px;
      height: 36px;
      padding: 0 30px 0 20px;
      border-radius: 8px;
      border: none;
      outline: 1px solid transparent;
      font-size: 14px;
      transition: all 0.2s;

      &:focus {
        outline-color: #999;
      }
    }

    .errors-wrap {
      height: 16px;
      margin: 8px;
      font-size: 12px;
      color: #e12c46;
    }

    .login-button {
      margin-top: 12px;
      height: 40px;
      width: 250px;
      background-color: $btn_bg_c;
      border: none;
      border-radius: 8px;
      color: $bg_c;
      font-size: 20px;
      cursor: pointer;
      text-align: center;
      transition: all 0.2s;

      &:hover {
        opacity: 0.8;
      }
    }
  }

  @media screen and (max-width: 900px) {
    .login-content {
      width: 320px;
      padding: 0;
    }
    .banner-box {
      display: none;
    }
    .login-box {
      position: initial;
      background-color: transparent;
    }
  }
</style>
